package org.demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.common.util.SF;

public abstract class ExcelWorkWriter<E> {
	public static void main(String[] args) throws Exception {
		ExcelWorkWriter<String> writer=new ExcelWorkWriter<String>() {

			@Override
			public void writeHeadRow() {
				writeHead('A', "value");
				writeHead('B', "default");
			}

			@Override
			public void writeDataRow(String data) {
				writeData('A',data);
				writeData('B', "112");
			}
			
		};
		writer.write("11111", new CacheIterator<String>() {
			int pageSize=5;
			int pageLen=10;
			int pageIndex=0;
			@Override
			public Iterator<String> fetch() {
				List<String> dd=new ArrayList<>();
				if(pageIndex++<pageLen) {
					for(int i=0;i<pageSize;i++) {
						dd.add(pageIndex+"-"+i);
					}
				}
				return dd.iterator();
			}
		});
		writer.write("22222", new CacheIterator<String>() {
			int pageSize=5;
			int pageLen=100;
			int pageIndex=0;
			@Override
			public Iterator<String> fetch() {
				List<String> dd=new ArrayList<>();
				if(pageIndex++<pageLen) {
					for(int i=0;i<pageSize;i++) {
						dd.add(pageIndex+"-"+i);
					}
				}
				return dd.iterator();
			}
		});
		FileOutputStream output=new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\0516\\123.xlsx"));
		writer.save(output);
	}
	public abstract void writeHeadRow();
	public void writeHead(char columnName,String head) {
		int colIndex=columnName-'A';
		sheet.setDefaultColumnStyle(colIndex, dataStyle);
		XSSFCell  cell=this.row.createCell(colIndex);
		//TODO 标题样式
		cell.setCellStyle(headStyle);
		cell.setCellValue(head);
	}
	public abstract void writeDataRow(E data);
	public void writeData(char columnName,String val) {
		int colIndex=columnName-'A';
		XSSFCell  cell=this.row.createCell(colIndex);
		if(SF.isNum(val)){
			cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
			if(SF.isIntValue(val)){
				cell.setCellValue(Long.valueOf(val));
			}else{
				cell.setCellValue(Double.valueOf(val));
			}
		}else{
			cell.setCellValue(val);
			cell.setCellType(XSSFCell.CELL_TYPE_STRING);
		}
	}
	XSSFWorkbook workbook;//document
	SXSSFWorkbook streamWorkbook;//file
	XSSFSheet sheet;//current sheet
	XSSFRow row; //current row
	XSSFCellStyle dataStyle; //data style
	XSSFCellStyle headStyle; //data style

	public ExcelWorkWriter() {
		workbook= new XSSFWorkbook();
		//使用streaming的xlsx减少系统内存消耗,使用临时文件会增加磁盘负担
	    streamWorkbook = new SXSSFWorkbook(workbook,100,false,true);
	    dataStyle=workbook.createCellStyle();
		DataFormat  format = workbook.createDataFormat();
		dataStyle.setDataFormat(format.getFormat("@"));
		XSSFFont font = workbook.createFont();
		font.setFontName("黑体");
		font.setFontHeightInPoints((short) 12);//设置字体大小
		dataStyle.setFont(font);
		headStyle=workbook.createCellStyle();
		 // 填充色
		headStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
		headStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
		XSSFFont headFont = workbook.createFont();
		headFont.setFontName("黑体");
		headFont.setFontHeightInPoints((short) 16);//设置字体大小
		headFont.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
		headStyle.setFont(headFont);

	}
	public  void  save(FileOutputStream output) throws IOException {
		streamWorkbook.write(output);
		output.flush();
		output.close();
		workbook.close();
		streamWorkbook.close();
	}

	public  void  write(String caption,Iterator<E> rowIterator) throws IOException {
		 sheet = workbook.createSheet(caption);
		 int rowIndex=0;
		row=sheet.createRow(rowIndex++);
		this.writeHeadRow();
		while(rowIterator.hasNext()) {
			 row=sheet.createRow(rowIndex++);
			E data=rowIterator.next();
			this.writeDataRow(data);
		}
	}
} 
